package com.cuit.blogsystem.controller.User;

import com.cuit.blogsystem.DTO.CommentDTO;
import com.cuit.blogsystem.DTO.TextDTO;
import com.cuit.blogsystem.Result.Result;
import com.cuit.blogsystem.Utils.JwtUtil;
import com.cuit.blogsystem.VO.UserVO;
import com.cuit.blogsystem.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cuit.blogsystem.service.userService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/blog/user")
public class UserController {

    @Autowired
    private userService userService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<UserVO> login(@RequestBody User user, HttpServletRequest httpServletRequest) throws Exception {
          User loginUser = userService.userLogin(user);
          Map<String, Object> claims = new HashMap<>();
          claims.put("userId", loginUser.getId());
          httpServletRequest.getSession().setAttribute("user", loginUser);
          String token = JwtUtil.createJWT(
                  36000000L,
                claims);

          UserVO userVO = new UserVO(loginUser.getId(), loginUser.getUsername(),token);

          return Result.success(userVO);
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result save(@RequestBody User user){
        userService.save(user);
        return Result.success("注册成功");
    }

    /**
     * 用户上传文章
     */
    @PostMapping("/upload")
    public Result upload(@RequestBody Text text, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        Long userId = user.getId();
        String username = user.getUsername();
        text.setUserId(userId);
        text.setUsername(username);
        userService.upload(text);
        request.getSession().setAttribute("text", text);
        return Result.success("上传成功");
    }

    /**
     * 用户删除文章
     */
    @PostMapping("/delete/{id}")
    public Result deleteText(HttpServletRequest request,@PathVariable Long id){
       userService.deleteText(id);
       return Result.success("删除完成");
    }

    /**
     * 用户修改文章
     */
    @PostMapping("/update")
    public Result updateText(@RequestBody TextDTO textDTO, HttpServletRequest request){
        userService.updateText(textDTO);
        return Result.success("修改完成");
    }

    /**
     * 用户评论
     */
    @PostMapping("/submit")
    public Result submit(Long id, String content, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        Long userId = user.getId();
        String username = user.getUsername();
        CommentDTO commentDTO = new CommentDTO(id, content, username, userId);
        userService.submit(commentDTO);
        return Result.success("评论成功");
    }

    /**
     * 用户修改评论
     */
    @PostMapping("/updateComment")
    public Result updateComment(@RequestBody CommentDTO commentDTO){
        userService.updateComment(commentDTO);
        return Result.success("修改成功");
    }

    /**
     * 用户删除评论
     */
    @PostMapping("/deleteComment/{id}")
    public Result deleteComment(@PathVariable Long id){
        userService.deleteComment(id);
        return Result.success("成功删除评论");
    }

    /**
     * 用户增加文章类型
     */
    @PostMapping("/addType")
    public Result addType(@RequestBody Type type){
        userService.addType(type);
        return Result.success("添加成功");
    }


    /**
     * 用户查看个人信息
     */
    @GetMapping("/checkMyself")
    public Result<User> checkMyself(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        User Myself =  userService.check(user.getId());
        return Result.success(Myself);
    }

    /**
     * 用户删除分类
     */
    @PostMapping("/deleteType/{category}")
    public Result deleteType(@PathVariable String category){
        userService.deleteType(category);
        return Result.success();
    }

}
